﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ParticleSystem;

namespace BGSpaceShooter
{
    public class Display
    {

        private static GraphicsDeviceManager graphicsDeviceManager;
        private static GraphicsDevice graphicsDevice;
        private static Matrix projectionMatrix;

        /// <summary>
        /// Zwraca graphics device (jedyny prawdziwy, słuszny, poprawny egzemplarz GraphicsDevice w systemie). Pole statyczne inicjalizowane przez konstruktor Display 
        /// w MainGame
        /// </summary>
        public static GraphicsDevice GraphicsDevice
        {
            get
            {
                if (graphicsDevice == null)
                {
                    throw new Exception("Graphics device not initialized");
                }
                return graphicsDevice;
            }
        }

        /// <summary>
        /// Zwraca graphics device (jedyny prawdziwy, słuszny, poprawny egzemplarz GraphicsDeviceManager w systemie). Pole statyczne inicjalizowane przez konstruktor Display 
        /// w MainGame
        /// </summary>
        public static GraphicsDeviceManager GraphicsDeviceManager
        {
            get
            {
                if (graphicsDeviceManager == null)
                {
                    throw new Exception("Graphics device manager not initialized");
                }
                return graphicsDeviceManager;
            }
        }

        /// <summary>
        /// Zwraca macierz rzutowania i. Pole statyczne inicjalizowane przez metode Initialize() w Display
        /// w MainGame
        /// </summary>
        public static Matrix ProjectionMatrix
        {
            get
            {
                if (projectionMatrix == null)
                {
                    throw new Exception("Projection matrix not created!");
                }
                return projectionMatrix;
            }
        }

        /// <summary>
        /// Inicjalizuje urzadzenie graficzne
        /// </summary>
        /// <param name="graphicsDeviceManager">referencja na GraphicsDeviceManagera z MainGame</param>
        public Display(GraphicsDeviceManager graphicsDeviceManager)
        {
            graphicsDevice = graphicsDeviceManager.GraphicsDevice;
            Display.graphicsDeviceManager = graphicsDeviceManager;
        }

        /// <summary>
        /// Przygotowuje macierz projekcji, ustawia globalne renderstate'y
        /// </summary>
        public void Initialize()
        {
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 0.5f, 6000.0f);

            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
            GraphicsDevice.BlendState = BlendState.Opaque;

        }


        public void Draw(Camera camera, List<IDrawer> drawers)
        {
            GraphicsDevice.Clear(Color.Azure);
            List<ParticleSystem.EffectDrawer> effectDrawers = new List<ParticleSystem.EffectDrawer>();

            foreach (IDrawer drawer in drawers)
            {
                drawer.Draw(camera);
                foreach (EffectDrawer effectDrawer in drawer.GetEffectDrawers())
                {
                    effectDrawers.Add(effectDrawer);
                }
            }

            foreach (EffectDrawer effectDrawer in effectDrawers)
            {
                effectDrawer.Draw(camera);
            }


        }
    }
}
